package com.pangdonghao.internationalizationdemo.exception;

import com.pangdonghao.internationalizationdemo.core.response.Meta;
import com.pangdonghao.internationalizationdemo.core.response.StatusCode;
import org.springframework.util.StringUtils;

@SuppressWarnings("serial")
public abstract class BaseException extends RuntimeException {

	protected StatusCode statusCode;

	public BaseException() {
	}

	public BaseException(Throwable ex) {
		super(ex);
	}

	public BaseException(String message) {
		super(message);
	}
	public BaseException(StatusCode statusCode) {
		this.statusCode = statusCode;
	}
	public BaseException(StatusCode statusCode, String message) {
		super(message);
		this.statusCode = statusCode;
	}

	public BaseException(String message, Throwable ex) {
		super(message, ex);
	}

	public Meta handler() {
		Meta meta = new Meta();
		meta.setSuccess(false);
		meta.setCode(getStatusCode().value());
		if (!StringUtils.isEmpty(getMessage())) {
			meta.setMsg(getMessage()); // 取系统的错误消息
		}else {
			meta.setMsg(getStatusCode().msg());
		}
		return meta;
	}

	protected abstract StatusCode getStatusCode();
}
